/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.db;

import uk.org.aspellclark.agenedb.gedcom.model.Family;
import uk.org.aspellclark.agenedb.gedcom.model.Header;
import uk.org.aspellclark.agenedb.gedcom.model.Individual;
import uk.org.aspellclark.common.db.AbstractDatabaseHelper;
import uk.org.aspellclark.common.db.AbstractDbConstants;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * a simple class used to determine whether the application needs to execute
 * in debug mode or not.
 *
 * @author andy
 */
/**
 * This class helps open, create, and upgrade the database file.
 */
public class DatabaseHelper extends AbstractDatabaseHelper {

	private boolean firstTimeThrough = true;
	private boolean createSampleDataOnStartup = false;

	public DatabaseHelper(Context context, AbstractDbConstants dbConstants, boolean createSamples) {
		super(context, dbConstants);
		this.createSampleDataOnStartup = createSamples;
	}

	private void createTables(SQLiteDatabase db) {
		db.execSQL(new Header().getSqlCreate());
		db.execSQL(new Individual().getSqlCreate());
		db.execSQL(new Family().getSqlCreate());
	}

	private void dropTables(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + new Header().getTableName());
		db.execSQL("DROP TABLE IF EXISTS " + new Individual().getTableName());
		db.execSQL("DROP TABLE IF EXISTS " + new Family().getTableName());
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		super.onCreate(db);
		this.createTables(db);
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		super.onUpgrade(db, oldVersion, newVersion);
		this.dropTables(db);
		this.onCreate(db);
	}

}
